$(document).ready(function() {   
    	$("#signup_form").click(function() {
          var name = $("#name").val();
          var password = $("#password").val();
          var email = $("#email").val();
          var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
          var captcha = $("#captcha").val();
          var data_signup = 'name=' + name + '&password=' + password + '&email=' + email + '&captcha=' + captcha; 
          var type = "POST";
          var url = "signup.php";
          var form = "#form_signup";

          if(name == "") {
            $(form + " > .nameError").slideDown('slow').delay(2000).slideUp('slow');
            $("#name").focus();
            return false;
          }

          if(password == "") {
            $(form + " > .passwordError").slideDown('slow').delay(2000).slideUp('slow');
            $("#password").focus();
            return false;
          }

          if(!email_regex.test(email)) {
            $(form + " > .emailError").slideDown('slow').delay(2000).slideUp('slow');
            $("#email").focus();
            return false;
          }

          if(captcha == "") {
            $(form + " > .captchaError").slideDown('slow').delay(2000).slideUp('slow');
            $("#captcha").focus();
            return false;
          }

          $("#signup > .loader").html("<img src='images/loader.gif'/>").fadeIn('fast');
          
          ajax_loader(type,url,data_signup);

          return false;
    	});

	$("#login_form").click(function() {
		    var name = $("#user-name").val();
        var password = $("#user-password").val();
        var data_login = 'name=' + name + '&password=' + password;
        var form = "#form_login";

        if(name == "") {
            $(form + " > .nameError").slideDown('slow').delay(2000).slideUp('slow');
            $("#user-name").focus();
            return false;
        }

        if(password == "") {
            $(form + " > .passwordError").slideDown('slow').delay(2000).slideUp('slow');
            $("#user-password").focus();
            return false;
        }

        $("#login > .loader").html("<img src='images/loader.gif'/>").fadeIn('fast');
        
        $.ajax({
          type: "POST",
          url: "login.php",
          data: data_login,
          success: function(data_success) {
              if(data_success == "true") {
                  $('.loader').fadeOut('fast');
                  document.location='page.php';
              } else {
                  $('.loader').fadeOut('fast');
                  $(".error").slideDown('slow').delay(3000).slideUp('slow');
              }
          }
      });

      return false;
	});

  $("#get_pass").click(function() {
      var name = $("#get-name").val();
      var email = $("#get-email").val();
      var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
      var data_get_pass = 'name=' + name + '&email=' + email;
      var type = "POST";
      var url = "get_pass.php";
      var form = "#get_pass_form";

      if(name == "") {
            $(form + " > .nameError").slideDown('slow').delay(2000).slideUp('slow');
            $("#get-name").focus();
            return false;
      }

      if(!email_regex.test(email)) {
            $(form + " > .emailError").slideDown('slow').delay(2000).slideUp('slow');
            $("#get-email").focus();
            return false;
      }

      $("#mail > .loader").html("<img src='images/loader.gif'/>").fadeIn('fast');

      ajax_loader(type,url,data_get_pass);

      return false;
  });

  function ajax_loader(type,url,data_form) {
      $.ajax({
          type: type,
          url: url,
          data: data_form,
          success: function(data_success) {
              if(data_success == "true") {
                  $('.loader').fadeOut('fast');
                  $(".success").slideDown('slow').delay(3000).slideUp('slow');
                        clear_form();
                        change_captcha();
              } else {
                  $('.loader').fadeOut('fast');
                  $(".error").slideDown('slow').delay(3000).slideUp('slow');
              }
          }
      });
  }

	function clear_form() {
        $("#name").val('');
        $("#email").val('');
        $("#password").val('');
        $("#captcha").val('');
        $("#get-name").val('');
        $("#get-email").val('');
    }

  $("#load_captcha").click(function() {
        change_captcha();
  });

  $("#getpass").click(function() {
        $("#mail > form").slideToggle('normal');
  });

  function change_captcha() {
        document.getElementById('img_captcha').src="captcha.php?rnd=" + Math.random();
  }

});